# -*- coding: utf-8 -*-
# Created by 'Zhou Bingbing'  on 2019/8/23
import itertools
import time

import jieba
import requests
from util.paixu import sort_words
from ac_auto.ac_auto import AutoResult
import itertools
# text='走势时刻雷吉杰克逊却从天而降改变了比赛的走势到了加时赛由于走势时刻之前拼的太凶小加早已精疲力竭只能眼睁睁看着雷霆将胜利抢走波洛'
# word='走势到了'
# print(AutoResult.main(word,text))
from config_setting.config_setting import url, session
from process_data.read_cilin_file import main
from util.jiebacut import cut
jieba.load_userdict("userdict.txt")
def strat_produce_seatence(sentence):
    #分词
    words=cut(sentence)
    #替换同义词 名词 动词
    res=[]
    for word in words:
        #同义词得替换
        if word.flag not in ['r','ul','ud','uj']:
            res.append(main(word.word))
        else:
            res.append([word.word])
    return res
#选取top 20 的词
def get_words_top3(listA,listB,i):
    if i==0:
        dictss={}
        for i in itertools.product(listA,listB):
            # 做top三的处理
            if len(i) == 2:
                words="".join(i)
                re=session.get(url=url.format(words))
                count=int(re.text)
                dictss["#".join(i)]=count
        return sort_words(dictss)
    else:
        dictss = {}
        for i in itertools.product(listA, listB):
            # 做top三的处理
            if len(i) == 2:
                # words = "".join(i)#在这里做处理
                words=i[0][0].split('#')[-1]+i[1]
                re = session.get(url=url.format(words))
                count = int(re.text)
                dictss[i[0][0]+"#"+i[1]] = count
        return sort_words(dictss)


def get_top_words_test_sentence(listA,listB):
    dictss = {}
    dicts = {}
    for i in itertools.product(listA, listB):
        if len(i) == 2:
            words = "".join(i)
            re = session.get(url=url.format(words))
            count = int(re.text)
            dictss["#".join(i)] = count
            dicts["#".join(i)] = count
    return sort_words(dictss),sort_words(dicts)
def corpus_generate_main(text):
    res=strat_produce_seatence(text)
    results=[]
    global words_3
    for i in range(len(res)):
        if i>len(res)-2:break
        if i==0:
            words_3=get_words_top3(res[i],res[i+1],i)
            results.append(words_3)
        else:
            words_3= get_words_top3(words_3, res[i+1],i) #两个字典一个做词频统计，一个文本做存储
            results.append(words_3)
    resultss= ["".join(i[0].split('#'))for i in results[-1]]
    return resultss

#利用句子通顺性判断我们的生成语料的合法性
def corpus_generate_test_sentence_main(text):
    res = strat_produce_seatence(text)
    results = []
    global words_3,next_words
    for i in range(len(res)):
        if i > len(res) - 2: break
        if i == 0:
            words_3,w= get_top_words_test_sentence(res[i], res[i + 1])
            next_words=[i[0].split('#')[-1]for i in words_3]
            results.append(w)
        else:
            words_3 ,w= get_top_words_test_sentence(next_words, res[i + 1])  # 两个字典一个做词频统计，一个文本做存储
            next_words = [i[0].split('#')[-1]for i in words_3]
            results.append(w)
    resuts_eventally=[]
    for i in range(len(results)):
        if i==len(results)-1:
            resuts_eventally.append(["".join(j[0].split('#'))for j in results[i]])
        else:
            resuts_eventally.append(list(set([j[0].split('#')[0]for j in results[i]])))
    return  get_eventally_text(resuts_eventally)
def get_eventally_text(results):
    res=[]
    for i in itertools.product(*results):
        res.append("".join(i))
    return res